package br.dot.droid.util;

import java.util.Calendar;
import java.util.Date;

public class StringUtil {

    public static String formatSQLDateTime(Calendar calendar) {
		char separador = '-';
		StringBuffer data = new StringBuffer();
		
		data.append(calendar.get(Calendar.YEAR)); 
		data.append(separador);
                int tmp = calendar.get(Calendar.MONTH) + 1;
		data.append((tmp > 9 ? "": "0" )+tmp); 
		data.append(separador); 
                
                tmp = calendar.get(Calendar.DAY_OF_MONTH);
		data.append((tmp > 9 ? "" : "0" )+tmp);
                
                data.append(' ');
                
                int n = calendar.get(Calendar.HOUR_OF_DAY);
                if (n < 10) {
                    data.append('0');
                }
                data.append(n);
                
                data.append(':');
                
                n = calendar.get(Calendar.MINUTE);
                if (n < 10) {
                    data.append('0');
                }
                data.append(n);
                
                data.append(':');
                
                n = calendar.get(Calendar.SECOND);
                if (n < 10) {
                    data.append('0');
                }
                data.append(n);
		
		// 
		return data.toString();
	}
    
    /**
     * Formata uma data para o formato "HH:mm"
     * @param date
     * @return
     */
    public static String formatToFriendlyDate(Date date){
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(date);
    	StringBuffer buffer = new StringBuffer();
    	int hours = calendar.get(Calendar.HOUR);
    	if(hours < 10){
    		buffer.append("0");
    	}
    	buffer.append(hours);
    	buffer.append(":");
    	int minutes = calendar.get(Calendar.MINUTE);
    	if(minutes < 10){
    		buffer.append("0");
    	}
    	buffer.append(minutes);
    	String dateHour = buffer.toString();
    	return dateHour;
    }
}
